package com.example.androidvue;

import android.net.http.SslError;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.androidvue.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);

        getSupportActionBar().hide();

        binding.button1.setOnClickListener(x-> binding.webview.loadUrl("https://www.baidu.com/"));
        binding.button2.setOnClickListener(x-> binding.webview.loadUrl("file:///android_asset/www/index.html"));
        binding.webview.setWebViewClient(new WebViewClient(){
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
                view.getSettings().setAllowFileAccess(true);
                super.onReceivedSslError(view, handler, error);
            }
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                Log.d("onReceivedError", "onReceivedError:" + error.getDescription() + " url:" + request.getUrl());
                super.onReceivedError(view, request, error);
            }
        });
//        binding.webview.getSettings().setAllowFileAccess(true);

        // 设置WebView属性
        WebSettings settings = binding.webview.getSettings();
//支持js
        settings.setJavaScriptEnabled(true);
//设置字符编码
        settings.setDefaultTextEncodingName("utf-8");
// 支持缩放
        settings.setSupportZoom(true);
// //启用内置缩放装置
        settings.setBuiltInZoomControls(true);
// 支持自动加载图片
        settings.setLoadsImagesAutomatically(true);
// 支持内容重新布局
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        //binding.webview.addJavascriptInterface(new CommodityData(getApplicationContext(), serializableMap, "ProductManageDetail"), "ProductManage");
    }
}